from tortoise.models import Model
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator


class Student(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description='姓名')
    pwd = fields.CharField(max_length=32, description='密码')
    sno = fields.IntField(description='学号', unique=True)

    clas = fields.ForeignKeyField('models.Clas', related_name='students', description='班级')
    courses = fields.ManyToManyField('models.Course', related_name='students', description='课程')

    class Meta:
        db_table = 'student'
    
    class PydanticMeta:
        exclude = ('pwd',)


StudentPydantic = pydantic_model_creator(Student, name='Student')
StudentPydanticIn = pydantic_model_creator(Student, name='StudentIn', exclude_readonly=True)


class Course(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description='课程名称')
    teacher = fields.ForeignKeyField('models.Teacher', related_name='courses', description='教师')
    addr = fields.CharField(max_length=32, description='上课地点')

    class Meta:
        db_table = 'course'


CoursePydantic = pydantic_model_creator(Course, name='Course')


class Clas(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description='班级名称')

    class Meta:
        db_table = 'clas'


ClasPydantic = pydantic_model_creator(Clas, name='Clas')

class Teacher(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description='教师名称')
    pwd = fields.CharField(max_length=32, description='密码')
    tno = fields.IntField(description='教师编号', unique=True)

    class Meta:
        db_table = 'teacher'


TeacherPydantic = pydantic_model_creator(Teacher, name='Teacher')
